//
//  EtasProcess.m
//  esand_ios_demo
//
//  Created by ReidLee on 2018/6/15.
//  Copyright © 2018年 esandinfo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "SimpleIFAAViewController.h"
#import "SimpleIFAAProcess.h"
#import <esand_cloud_ifaa/esand_cloud_ifaa.h>
#import <UIKit/UIKit.h>
#import "HttpClient.h"

// SIMPLE IFAA 阿里云地址(固定)
#define SIMPLE_IFAA_ALIYUN_URL @"http://simpleifaa.market.alicloudapi.com/ifaa/sAuth"

// FIMPLE IFAA 流程
@implementation SimpleIFAAProcess
{
    SimpleIFAAViewController* _vController;
}

// 初始化
- (id) initWithVController:(SimpleIFAAViewController *) vController  authtype:(int) authtype{
    self = [super init];
    
    if (self) {
        self->_vController = vController;
    }

    return self;
}

// SIMPLE IFAA 认证
- (void) auth {
    NSString *is = [IFAAManager isSupportIFAA];
    int authType;
    if ([is isEqualToString:IFAA_STATUS_FINGERPRINT]) {
        authType = AUTHTYPE_FINGERPRINT;
    } else if ([is isEqualToString:IFAA_STATUS_FACE_ID]) {
        authType = AUTHTYPE_FACEID;
    } else {
        [_vController logMsg:@"此设备不支持IFAA"];
        return;
    }
    
    IFAABaseInfo * _baseInfo = [[IFAABaseInfo alloc] initWithAuthType: authType
                                                       userId:@"mno"
                                                      transId:@"sIFAA-ios"
                                                 transPayload: @""
                                                    transType:@"sIFAA-ios"
                                                  reasonTitle: @"指纹认证"
                                                fallbackTitle: @"指纹认证"
                                                   ifaaAction: IFAA_SIMPLE];
    // 执行IFAA 本地认证
    [IFAAManager sAuth:_baseInfo ifaaCallback:^(IFAAResult *ifaaResult) {
        // 认证完成，认证结果回调
        NSString* code = ifaaResult.code;
        NSString* msg = ifaaResult.msg;
        // 认证结果判断
        if ([code isEqualToString: IFAA_STATUS_NOT_SUPPORT]) {
            [_vController logMsg:@"此设备不支持IFAA(8.0以上的版本支持)"];
        } else if ([code isEqualToString: IFAA_STATUS_NOT_ENROLLED]) {
            // TODO 引导用户录入指纹/人脸后再使用
            [_vController logMsg:@"尚未录入指纹/人脸，录入后方可使用过"];
        } else if ([code isEqualToString: IFAA_CLIENT_ERROR]) {
            [_vController logMsg:@"本地代码异常"];
        } else if ([code isEqualToString: IFAA_PERMISSION_DENIED]) {
            // TODO 引导用户授权后使用
            [_vController logMsg:@"未获得人脸权限,需引导用户去获得授权人脸权限"];
        } else if ([code isEqualToString: IFAA_STATUS_PASSCODE_NOT_SET]) {
            // TODO 引导用户设置屏幕锁后使用
            [_vController logMsg:@"屏幕锁未设置"];
        } else if ([code isEqualToString:IFAA_STATUS_RESULT_AUTH_FAIL]) {
            [_vController logMsg:@"认证失败"];
        } else if([code isEqualToString:IFAA_STATUS_RESULT_CANCELED]){
            [_vController logMsg:@"用户已取消认证操作"];
        } else if([code isEqualToString:IFAA_CLIENT_ERROR]){
            [_vController logMsg:@"执行异常"];
        } else if([code isEqualToString:IFAA_SERVER_ERROR]){
            [_vController logMsg: msg];
        } else if([code isEqualToString: IFAA_NOT_AVAILABLE ]){
            [_vController logMsg:@"当前不可用"];
        } else if ([code isEqualToString:IFAA_SUCCESS]) {
            // TODO 认证成功，发起服务器端认证操作，此段逻辑在实际项目中应该发送给业务服务器，再由业务服务器发起(保护好APPCODE）
            HttpClient* httpClient = [[HttpClient alloc] initWithNSURL:[NSURL URLWithString: SIMPLE_IFAA_ALIYUN_URL]];
            // TODO 更换成您正式的APPCODE
            NSString* ALIYUN_APPCODE = @"2dcd5b9f926f4bb7a3d88495d1990944";//@"填入您自己的appcode";
            [httpClient requestSync:[NSString stringWithFormat:@"ifaaMsg=%@",ifaaResult.msg] appCode: ALIYUN_APPCODE clientCallback:^(NSString *resultStr) {
                [_vController logMsg:@">>> 认证"];
               [_vController logMsg:resultStr];
            }];
        } else if ([code isEqualToString:IFAA_SUCCESS_REG]){
            //内部注册成功不需要做网络请求
            [_vController logMsg:@">>> 注册"];
            [_vController logMsg:msg];
        }else {
           [_vController logMsg:[NSString stringWithFormat:@"认证失败（其他异常） %@ : %@",code,msg]];
        }
    }];
}

@end
